<?php 
class Controller_System_Menu extends ypController {
	public function Index() {
		$this->loadLanguage('System/Menu');

		// Active
		$code = strtolower($this->Link->this());
		if (strpos($code, 'news')) $link['news']['active'] = true;
		elseif (strpos($code, 'news')) $link['news']['active'] = true;
		elseif (strpos($code, 'download')) $link['download']['active'] = true;
		elseif (strpos($code, 'user')) $link['account']['active'] = true;
		elseif (strpos($code, 'contact')) $link['contact']['active'] = true;
		else $link['home']['active'] = true;

		
		// Top link
		$link['home']['head'] = $this->Link->a('/', $this->_data['text_menu_home']);
		$link['news']['head'] = $this->buildMenu('News/Index', $this->_data['text_menu_news'], TRUE);
			$link['news']['sub']['cat'] = $this->Link->a('News/Cat', $this->_data['text_menu_news_cat']);
			$link['news']['sub']['newest'] = $this->Link->a('News/Newest', $this->_data['text_menu_news_newest']);
		$link['download']['head'] = $this->buildMenu('Download', $this->_data['text_menu_download'], TRUE);
			$link['download']['sub']['source'] = $this->Link->a('Download/Source', $this->_data['text_menu_download_source']);
			$link['download']['sub']['module'] = $this->Link->a('Download/Module', $this->_data['text_menu_download_module']);
			$link['download']['sub']['language'] = $this->Link->a('Download/Language', $this->_data['text_menu_download_language']);
			$link['download']['sub']['software'] = $this->Link->a('Download/Software', $this->_data['text_menu_download_software']);
		if ($this->User->isLogin()) {
			$link['account']['head'] = $this->buildMenu('User/Account', $this->_data['text_menu_account'], TRUE);
				$link['account']['sub']['info'] = $this->Link->a('User/Account', $this->_data['text_menu_account_info']);
			if ($this->User->isAdmin()) {
				$link['account']['sub']['admin'] = $this->Link->a('Admin/Index/Home', $this->_data['text_menu_account_admin']);
			}
				$return = urlencode($this->Link->build($this->Link->this(), TRUE, $this->Link->getParams()));
				$link['account']['sub']['logout'] = $this->Link->a(array('User/Logout', array('redirect' => $return)), $this->_data['text_menu_account_logout']);
		} else {
			$link['account']['head'] = $this->Link->a('Login/Index', $this->_data['text_menu_login']);
		}
		$link['contact']['head'] = $this->Link->a('Contact', $this->_data['text_menu_contact']);

		$this->set('links', $link);
		
		$this->setTemplate('layout/Menu.php');
		$this->render();
	}
	
	public function buildMenu($link, $text, $sub = FALSE) {
		if ($sub == FALSE) {
			return $this->Link->a($link, $text);
		}
		
		$link = '<a class="dropdown-toggle" href="'. $this->Link->build($link) .'">';
		$link .= $text . ' <b class="caret"></b></a>';
		return $link;
	}
}